from lib.config.base import BaseConfig
from utils import common
import json


class MqttConfig(BaseConfig):
    def __init__(self, path):
        super().__init__(path)
        self.__lastwill_topic = "esp32lost"
        self.__lastwill_msg = json.dumps(
            {"ClientID": self.get_ClientID(), "user": self.get_user()}
        )

    def is_debug(self):
        return common.get_data_value(self.__config, "debug") == 1

    def get_server(self):
        return common.get_data_value(self.__config, "server")

    def get_user(self):
        return common.get_data_value(self.__config, "user")

    def get_password(self):
        return common.get_data_value(self.__config, "password")

    def get_topic(self):
        return common.get_data_value(self.__config, "topic")

    def get_ClientID(self):
        return common.get_data_value(self.__config, "ClientID")

    def get_port(self):
        return common.get_data_value(self.__config, "port")

    def get_protocol(self):
        return common.get_data_value(self.__config, "protocol")

    def get_keepalive(self):
        return common.get_data_value(self.__config, "keepalive") or 60
    
    def get_ssl(self):
        return common.get_data_value(self.__config, "ssl") or 0

    def get_lastwill_topic(self):
        return (
            common.get_data_value(self.__config, "lastwill_topic")
            or self.__lastwill_topic
        )

    def get_lastwill_msg(self):
        return self.__lastwill_msg
